package com.wwk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wwk.constant.MessageConstant;
import com.wwk.entity.Result;
import com.wwk.pojo.OrderSetting;
import com.wwk.service.OrderSettingService;
import com.wwk.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Classname OrderSettingController预约设置
 * @Description TODO
 * @Date 2020/11/4 16:20
 * @Created by com.wwk
 * @Author com.wwk
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService settingService;

    //文件上传，实现预约设置数据上传到服务器
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile){
        try {
            //使用poi通用方法解析表格数据
            final List<String[]> excel = POIUtils.readExcel(multipartFile);
            //创建新的集合，用于将表格数据转换为OrderSetting集合
            List<OrderSetting> list=new ArrayList<>();
            if(excel != null && excel.size() > 0){
                for (String[] strings : excel) {
                    Date orderDate=new Date(strings[0]);
                    Integer number=Integer.valueOf(strings[1]);
                    OrderSetting orderSetting=new OrderSetting(orderDate,number);
                    list.add(orderSetting);
                }
            }
            //调用dubbo远程调用服务将数据存入数据库
            settingService.add(list);
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            //文件解析失败
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/getOrderSettingByDate")
    public Result getOrderSettingByDate(String date){//date 格式为2020-11
        System.out.println(date);
        try{
            //这里选择Map而不是OrderSetting的原因是，客户端接收的数据的格式和OrderSetting的格式不同，需要灵活的添加
                    /*this.leftobj = [
                        { date: 1, number: 120, reservations: 1 },
                        { date: 3, number: 120, reservations: 1 },
                        { date: 4, number: 120, reservations: 120 },
                        { date: 6, number: 120, reservations: 1 },
                        { date: 8, number: 120, reservations: 1 }
                    ];*/
            List<Map> ordersettings=settingService.getOrderSettingByDate(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,ordersettings);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try{
           settingService.editNumberByDate(orderSetting);
           return new Result(true,"更改可预约人数成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,"更改可预约人数失败!");
        }

    }
}
